<template>
  <!-- 表格 -->
  <ele-pro-table
    ref="tableRef"
    row-key="deviceId"
    :columns="columns"
    :datasource="datasource"
    height="calc(100vh - 290px)"
    tool-class="ele-toolbar-form"
    :scroll="{ x: 800 }"
    tools-theme="default"
    bordered
    cache-key="proSystemOrgUserTable"
    class="sys-org-table"
  >
    <template #toolbar>
      <org-user-search @search="reload" @add="openEdit()" />
    </template>
    <template #bodyCell="{ column, record }">
      <template v-if="column.key === 'roles'">
        <a-tag v-for="item in record.roles" :key="item.roleId" color="blue">
          {{ item.roleName }}
        </a-tag>
      </template>
      <template v-else-if="column.key === 'action'">
        <a-space>
          <a @click="openEdit(record)">修改</a>
          <a-divider type="vertical" />
          <a-popconfirm
            placement="topRight"
            title="确定要删除此账号吗？"
            @confirm="remove(record)"
          >
            <a class="ele-text-danger">删除</a>
          </a-popconfirm>
        </a-space>
      </template>
    </template>
    <!-- 编辑弹窗 -->
    <device-edit
      :data="current"
      v-model:visible="showEdit"
      :all-group-list="props.allGroupList"
      :group-id="props.groupId"
      :group-org="props.groupOrg"
      @done="reload"
    />
  </ele-pro-table>
</template>

<script setup>
  import { ref, watch } from 'vue';
  import { message } from 'ant-design-vue/es';
  import { messageLoading, toDateString } from 'ele-admin-pro/es';
  import OrgUserSearch from './group-device-search.vue';
  import DeviceEdit from './device-edit.vue';
  import { pageDevices, removeDevice } from '@/api/audio/device';

  const props = defineProps({
    // 机构 id
    groupId: Number,
    // 全部对讲组
    allGroupList: Array,
    // 当前所属组织g
    groupOrg: String,
    // 当前选中对讲组
    groupName: String
  });

  // 表格实例
  const tableRef = ref(null);

  // 表格列配置
  const columns = ref([
    {
      key: 'index',
      width: 48,
      align: 'center',
      fixed: 'left',
      hideInSetting: true,
      customRender: ({ index }) => index + (tableRef.value?.tableIndex ?? 0)
    },
    {
      title: '对讲机账号',
      dataIndex: 'deviceNo',
      sorter: true,
      showSorterTooltip: false
    },
    {
      title: '所属组织',
      dataIndex: 'deviceOrg',
      sorter: true,
      showSorterTooltip: false
    },
    {
      title: '设备型号',
      dataIndex: 'deviceModel',
      sorter: true,
      showSorterTooltip: false
    },
    {
      title: '所在对讲组',
      dataIndex: 'groupList',
      width: 180,
      sorter: true,
      showSorterTooltip: false,
      customRender:({record }) => {
        return record .groupList?.map(item => item.groupName).join(',');
      }
    },
    {
      title: '注册时间',
      dataIndex: 'registerTime',
      width: 180,
      sorter: true,
      showSorterTooltip: false
    },
    {
      title: '创建时间',
      dataIndex: 'createTime',
      sorter: true,
      showSorterTooltip: false,
      ellipsis: true,
      customRender: ({ text }) => toDateString(text)
    },
    {
      title: '操作',
      key: 'action',
      width: 100,
      align: 'center'
    }
  ]);

  // 当前编辑数据
  const current = ref(null);

  // 是否显示编辑弹窗
  const showEdit = ref(false);

  // 表格数据源
;  const datasource = ({ page, limit, where, orders }) => {
    return pageDevices({
      ...where,
      ...orders,
      page,
      limit,
      groupId: props.groupId,
      groupOrg: props.groupOrg
    });
  }

  /* 搜索 */
  const reload = (where) => {
    tableRef?.value?.reload({ page: 1, where });
  };

  /* 打开编辑弹窗 */
  const openEdit = (row) => {
    current.value = row ? { ...row } : null;
    showEdit.value = true;
    console.log("openEdit======value===>",current.value);
  };

  /* 删除单个 */
  const remove = (row) => {
    const hide = messageLoading('请求中..', 0);
    removeDevice(row.deviceId)
      .then((msg) => {
        hide();
        message.success(msg);
        reload();
      })
      .catch((e) => {
        hide();
        message.error(e.message);
      });
  };

  // 监听机构 id 变化
  watch(
    () => props.groupId,
    () => {
      reload();
    }
  );
</script>

<style lang="less" scoped>
  .sys-org-table :deep(.ant-table-body) {
    overflow: auto !important;
    overflow: overlay !important;
  }

  .sys-org-table :deep(.ant-table-pagination.ant-pagination) {
    padding: 0 4px;
    margin-bottom: 0;
  }
</style>
